<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>分类管理</title>
  <link rel="stylesheet" href="/static/lib/layui/css/layui.css">
</head>
<body>
  <div class="layui-container-fluid"  style="margin: 20px">
    <div class="nav" style="margin-bottom:20px">
      <span class="layui-breadcrumb" >
        <a href="/dashBoard">首页</a>
        <a><cite>分类管理</cite></a>
      </span> 
    </div>
    <a href="/cateAdd" class="layui-btn layui-btn-normal">增加</a>
    <table class="layui-table">
      <thead>
        <tr>
          <th>分类名称</th>
          <th>分类描述</th>
          <th>分类图标</th>
          <th>操作</th>
        </tr> 
      </thead>
      <tbody>
        <% if(!cates.length) { %>
          <tr>
            <td align="center" colspan="4">暂无分类</td>
          </tr>
        <% } %>
        
        <% for(var i = 0; i< cates.length; i++) { %>
          <tr>
            <td><%= cates[i].cateName %></td>
            <td title="<%= cates[i].cateDesc  %>"><%= cates[i].cateDesc ? cates[i].cateDesc.substr(0,10)+'...': '暂无描述' %></td>
            <td>
              <img src="<%= cates[i].cateIcon || 'https://dss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1771023030,733807280&fm=26&gp=0.jpg' %>" width="100" alt="">
            </td>
            <td>
              <a href="/cateUpdate?id=<%= cates[i]._id %>" class="layui-btn layui-btn-xs">编辑</a>
              <button  _id="<%= cates[i]._id %>" class="layui-btn del layui-btn-danger layui-btn-xs">删除</button>
            </td>
          </tr>
        <%}%>
      </tbody>
    </table>
    <div id="page"></div>      
  </div>
  <script src="/static/lib/layui/layui.js"></script>
  <script>
    layui.use(['laypage', 'jquery', 'layer'], function(){
      var laypage = layui.laypage;
      var $ = layui.jquery;
      var layer = layui.layer;
    //执行一个laypage实例
    laypage.render({
        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
        ,count: <%= counts %> //数据总数，从服务端得到
        ,limit: 2
        ,curr:<%= current %>
        ,jump: function(obj, first){
          //obj包含了当前分页的所有参数，比如：
          //首次不执行
          if(!first){
            //do something
            location.href = "/cateList?current="+obj.curr+"&pageSize="+obj.limit;
          }
        }
      });
      // 删除分类
      $('.del').click(function(){
        var id = $(this).attr('_id');
        layer.confirm('删除分类?', {icon: 3, title:'您确认删除吗'}, function(index){
          //do something
          // 发ajax请求删除 分类
          $.ajax({
            url: '/delCate',
            type: 'POST',
            dataType: 'json',
            data: {
              id: id
            },
            success: function(res){
              if(res.code === 0) {
                // 删除成功
                layer.msg(res.msg, {
                  icon: 1,
                  time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function(){
                  history.go(0)
                });
              }else{
                // 删除失败
                layer.msg(res.msg, {
                  icon: 2,
                  time: 1000 //2秒关闭（如果不配置，默认是3秒）
                });
              }
            }
          })
          layer.close(index);
        });
      })
    })
  </script>
</body>
</html>